package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.domain.AlpContractDetail;
import com.ruoyi.system.domain.AlpContractStage;
import com.ruoyi.system.service.IAlpContractDetailService;
import com.ruoyi.system.service.IAlpContractStageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AlpStageActual;
import com.ruoyi.system.service.IAlpStageActualService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 实际收款Controller
 * 
 * @author zhangjun
 * @date 2020-02-15
 */
@Controller
@RequestMapping("/system/actual")
public class AlpStageActualController extends BaseController
{
    private String prefix = "system/actual";

    @Autowired
    private IAlpStageActualService alpStageActualService;

    @Autowired
    private IAlpContractDetailService alpContractDetailService;

    @Autowired
    private IAlpContractStageService alpContractStageService;

    @RequiresPermissions("system:actual:view")
    @GetMapping()
    public String actual()
    {
        return prefix + "/actual";
    }

    /**
     * 查询实际收款列表
     */
    @RequiresPermissions("system:actual:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AlpStageActual alpStageActual)
    {
        startPage();
        List<AlpStageActual> list = alpStageActualService.selectAlpStageActualList(alpStageActual);
        return getDataTable(list);
    }



    /**
     * 导出实际收款列表
     */
    @RequiresPermissions("system:actual:export")
    @Log(title = "实际收款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AlpStageActual alpStageActual)
    {
        List<AlpStageActual> list = alpStageActualService.selectAlpStageActualList(alpStageActual);
        ExcelUtil<AlpStageActual> util = new ExcelUtil<AlpStageActual>(AlpStageActual.class);
        return util.exportExcel(list, "actual");
    }

    /**
     * 新增实际收款
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增实际收款
     */
    @GetMapping("/add/{stageId}")
    public String add(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        mmap.put("alpStageDetail", alpContractStageService.getStageDetail(stageId));
        return prefix + "/addFromStage";
    }






    /**
     * 可编辑表格--编辑实际金额
     */
    @GetMapping("/editactual/{stageId}")
    public String editTableActual(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        mmap.put("stageId", stageId);
        return prefix + "/editableActual";
    }


    /**
     * 查询实际收款列表
     */
    @RequiresPermissions("system:actual:list")
    @PostMapping("/listTable/{stageId}")
    @ResponseBody
    public TableDataInfo listTableActual(@PathVariable("stageId") Long stageId, ModelMap mmap)
    {
        startPage();
        List<AlpStageActual> list = alpStageActualService.selectAlpStageActualByStageId(stageId);
        return getDataTable(list);
    }

    /**
     * 修改保存实际收款
     */
    @RequiresPermissions("system:actual:edit")
    @Log(title = "实际收款", businessType = BusinessType.UPDATE)
    @PostMapping("/editOneLine/{id}/{actualMoney}/{actualTime}")
    @ResponseBody
    public AjaxResult editOneLine(@PathVariable("id") Long id,
                                  @PathVariable("actualMoney") BigDecimal actualMoney,
                                  @PathVariable("actualTime") Date actualTime)
    {
        AlpStageActual actual = alpStageActualService.selectAlpStageActualById(id); //必须在前
        //重新插入一条
        actual.setId(null);
        if (null != actualMoney) {
            actual.setActualMoney(actualMoney);
        }
        if (null != actualTime) {
            actual.setActualTime(actualTime);
        }
        actual.setUpdatedTime(new Date());
        //将原来的记录 置删除标记为1
        alpStageActualService.deleteAlpStageActualById(id);//必须在后

        return toAjax(alpStageActualService.insertAlpStageActual(actual));
    }

    /**
     * 新增保存实际收款
     */
    @RequiresPermissions("system:actual:add")
    @Log(title = "实际收款", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AlpStageActual alpStageActual)
    {
        alpStageActual.setUpdatedTime(new Date());
        alpStageActual.setDelFlag(0);
        return toAjax(alpStageActualService.insertAlpStageActual(alpStageActual));
    }

//    /**
//     * 指定stageId 新增保存实际收款
//     */
//    @RequiresPermissions("system:actual:add")
//    @Log(title = "实际收款", businessType = BusinessType.INSERT)
//    @PostMapping("/add/{stageId}")
//    @ResponseBody
//    public AjaxResult addSaveByStageId(AlpStageActual alpStageActual)
//    {
//        alpStageActual.setDelFlag(0);
//        return toAjax(alpStageActualService.insertAlpStageActual(alpStageActual));
//    }

    /**
     * 修改实际收款
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        AlpStageActual alpStageActual = alpStageActualService.selectAlpStageActualById(id);
        mmap.put("alpStageActual", alpStageActual);
        return prefix + "/edit";
    }

    /**
     * 修改保存实际收款
     */
    @RequiresPermissions("system:actual:edit")
    @Log(title = "实际收款", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AlpStageActual alpStageActual)
    {
        return toAjax(alpStageActualService.updateAlpStageActual(alpStageActual));
    }

    /**
     * 删除实际收款
     */
    @RequiresPermissions("system:actual:remove")
    @Log(title = "实际收款", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(alpStageActualService.deleteAlpStageActualByIds(ids));
    }

    /**
     * 删除实际收款
     */
    @RequiresPermissions("system:predict:remove")
    @PostMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult removeById(@PathVariable("id") Long id)
    {
        return toAjax(alpStageActualService.deleteAlpStageActualById(id));
    }

    /**
     * 查询预测收款列表
     */
    @RequiresPermissions("system:contractDetail:moneyBack")
    @PostMapping("/listActualTable/{contractId}")
    @ResponseBody
    public TableDataInfo listActualTable(@PathVariable("contractId") Long contractId, ModelMap mmap)
    {
        startPage();
        AlpStageActual alpStageActual = new AlpStageActual();
        alpStageActual.setContractId(contractId);
        List<AlpStageActual> list = alpStageActualService.selectAlpStageActualList(alpStageActual);
        return getDataTable(list);
    }
}
